var ws, arrId, nums, zongji, aid, uid, spage, wtype, vice;
document.addEventListener('plusready',function(){
	uid = plus.storage.getItem('uid');
	ws = plus.webview.currentWebview();
	arrId = ws.arrId;
	nums = ws.nums;
	zongji = ws.zongji;
	spage = ws.spage;
	aid = ws.aid;//病例Id
	wtype = ws.wtype;
	vice = ws.vice;//几副药
	console.log(aid+"  "+arrId+"  "+nums+"  "+zongji+"  "+uid+" "+wtype+" "+vice);
	$('.zongji').text(zongji);
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=Medicinal&a=proList&arrId="+arrId,
		dataType:'json',
		success :function(data){
//			console.log(JSON.stringify(data));
			plus.nativeUI.closeWaiting();
			if(data){
				var list = '';
				nums = nums.split(',');
				$.each(data, function(k,v) {
					list += '<div class="mui-table-view medicine-lists overflow-h mui-table-view-cell mui-checkbox mui-left delgood'+v.id+'"">'+
								'<div><img src="'+getAvatar(v.litpic)+'" alt="" /></div>'+
								'<div><span>'+v.title+'</span><span class="mui-pull-right mui-h5" pri="'+v.price+'" tit="'+v.title+'">'+nums[k]+'克</span></div>'+
								'<input class="checkbox-med" name="checkbox" value="'+v.id+'" type="checkbox" style="display: none;">'+
							'</div>';
				});
				$('.medicineList').html(list);
				$(".medicine-lists").removeClass("mui-left");
			}
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
	//点击编辑、删除按钮事件
	$(".bj").on('click',function(){
		if($(this).text()=="编辑"){
			$(".checkbox-med").show();
			$(".medicine-lists").addClass("mui-left");
			$(this).text('删除');
		}else{
			//获取选中的商品id
			var arrId=""; 
			$("input[type='checkbox'][class='checkbox-med']:checked").each(function(){
				arrId += $(this).val() + ",";   
			})
			if(arrId == ''){
				toast('未选中处方');return;
			}
			var strs= new Array(); //定义一数组 
			strs=arrId.split(","); //字符分割 
//			console.log(strs);
			for (i=0;i<strs.length ;i++ ){ 
				$('.delgood'+strs[i]).remove();
			} 
			var zj = 0;//删除处方后改变总价
			$('.mui-h5').each(function(){
				var price = $(this).attr('pri');
				var num = $(this).text();
//				console.log(price+" "+num);
				zj += parseFloat(price)*parseInt(num);
			})
			$('.zongji').text(zj.toFixed(2));
			$(".checkbox-med").hide();
			$(".medicine-lists").removeClass("mui-left");
			$(this).text('编辑');
		} 
	});
	$('.tijiao').click(function(){
		var chu = '';
		$('.mui-h5').each(function(){
			var tit = $(this).attr('tit');
			var num = $(this).text();
			chu += tit + ',' + num + ';';
		})
		var total = $('.zongji').text();
		if(chu == '' || total == '0元'){
			toast('无处方');return;
		}
		total = parseFloat(total);
		console.log(aid+" "+chu+" "+total+" "+uid);
		//订单号
		var ordernum = uid+new Date().getTime();
		showWating();
		$.ajax({
			type:"get",
			url:apiRoot+"?m=home&c=Medicinal&a=savePro",
			data :{
				aid: aid,
				recipe: chu,
				total: total,
				did: uid,
				ordernum :ordernum,
				wtype :wtype,
				status :0,
				vice :vice
			},
			dataType :'json',
			success :function(data){
				console.log(JSON.stringify(data));
				plus.nativeUI.closeWaiting();
				if(data > 0){
					toast('提交成功');
					if(spage){
						plus.webview.getWebviewById(spage).close();
					}
					setTimeout(function(){
						ws.close();
					},2000);
				}
			},
			error :function(e){
				console.log(JSON.stringify(e));
				errortoast();
			}
		});
	})
},false)